<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include/src :: form_base_js_and_css"/>
</head>
<body style="background-color: #f2f2f2; height: 100%;">

<div id="id_panel">
</div>
<script th:inline="javascript">
	// 表单初始化
	var formObj = null;
	//使用layui的sku模块
	layui.config({
		//自定义layui组件的目录,设定扩展的layui模块的所在目录，一般用于外部模块扩展
		base: '/static/js/layui_extends/',
		version: true
	}).use(['form', 'jquery', 'laydate', 'YsForm', 'YsModal'], function(){
		var $ = layui.jquery,
				form = layui.form,
				laydate = layui.laydate,
				YsForm = layui.YsForm,
				YsModal = layui.YsModal;
		/*
		* 表单初始化
		* 参数1：表单配置参数
		* 参数2：子表配置参数
		*/
		formObj = YsForm.init(
				{
					id:'id_panel', // 页面容器ID
					dto: $.parseJSON(JSON.stringify([[${dto}]]))
				}
		);

		/**
		 * 监听兑换方式 TODO 未完善
		 */
		var domForm1 = $('#' + formObj.c.id_form);
		var domPanel = $('#' + formObj.c.id);

		var effectStock = formObj.getValueByName("effectStock");
		if(effectStock!=1){
			formObj.matchValuesAfterModalSelectRadio=function (colCode, rowSelected, extraData) { // 单选框选中数据后，匹配表单数值
				var t = formObj;
				var domForm = $('#' + t.c.id_form);
				console.log('rowSelected',rowSelected);
				var reconciliationBid = formObj.getValueByName("reconciliationBid");
				for(var i = 0; i < t.c.dto.rows.length; i++){
					var col = t.c.dto.rows[i];
					if (col.code == colCode) {
						// 给选择框设置的字段赋值
						domForm.find('input[name="' + colCode + '"]').val(rowSelected[col.modalValueCode]);
					} else if(col.modalRelativeCode == colCode) {
						domForm.find('input[name="' + col.code + '"]').val(rowSelected[col.modalValueCode]);
					}
				}
				var bid = rowSelected.bid?rowSelected.bid:reconciliationBid;
				var exchangeRateByGbp = formObj.getValueByName("exchangeRateByGbp");
				var exchangeRateByUsd = formObj.getValueByName("exchangeRateByUsd");
				var exchangeRateByEur = formObj.getValueByName("exchangeRateByEur");
				//if(bid!=reconciliationBid){//如果新调取，则删除列表信息，并重新获取汇率等计算信息
					$.ajax({
						type: "POST",
						url: '/api/inter/tcwWanliPrepaymentRecorded/toCountInfo',
						data: {
							'bid': formObj.getValueByName("bid"),
							'reconciliationBid': bid,
							//'depositPlatformCode': rowSelected.code,
							'exchangeRateByGbp':exchangeRateByGbp,
							'exchangeRateByUsd':exchangeRateByUsd,
							'exchangeRateByEur':exchangeRateByEur
						},
						success: function (data) {
							console.log('data',data);
							if (data.code == 0) {
								formObj.setValueByName('exchangeRateByGbp', data.exchangeRateByGbp);
								formObj.setValueByName('exchangeRateByUsd', data.exchangeRateByUsd);
								formObj.setValueByName('exchangeRateByEur', data.exchangeRateByEur);
								formObj.setValueByName('poolingTotal', data.poolingTotal);
								formObj.setValueByName('reconciliationFeeTotal', data.reconciliationFeeTotal);
								formObj.setValueByName('countFeeTotal', data.countFeeTotal);
								formObj.setValueByName('recordedTotal', data.recordedTotal);
								formObj.setValueByName('recordedOperations', data.recordedOperations);
								formObj.setValueByName('recordedFinance', data.recordedFinance);
								formObj.setValueByName('amountCny', data.amountCny);
								console.log('formObj',formObj);

								//获取子列表信息
								var newSonList = [];
								var sonList = data.sonList;
								if(sonList!=null && sonList.length>0){
									for(var i=0;i<sonList.length;i++){
										var row = sonList[i];
										row.itemId = "";
										row.reconciliationItemId = row.id;
										var exchangeRate = 0;
										var recordAmountGuiji = parseFloat(row.recordAmountGuiji);//记录归集金额
										var jyCurrencyCode = row.jyCurrencyCode;//币种
										if(jyCurrencyCode=="GBP"){
											exchangeRate = parseFloat(domForm.find('input[name="exchangeRateByGbp"]').val());
										}else if(jyCurrencyCode=="USD"){
											exchangeRate = parseFloat(domForm.find('input[name="exchangeRateByUsd"]').val());
										}else if(jyCurrencyCode=="EUR"){
											exchangeRate = parseFloat(domForm.find('input[name="exchangeRateByEur"]').val());
										}
										//计算入账金额（CNY） = 记录归集金额*对应币种的计算汇率*0.998*0.98
										var recordedAmount = recordAmountGuiji*exchangeRate*0.998*0.98;
										row.recordedAmount = recordedAmount.toFixed(2);
										newSonList.push(row);
									}
								}

								formObj.c.subGridObj.c.sonlist = newSonList;

								var recordedTotal = parseFloat(domForm.find('input[name="recordedTotal"]').val());//总入账金额（CNY）
								var recordedOperations = 0;//运营入账金额（CNY） = 子表入账金额之和
								for(var i=0;i<formObj.c.subGridObj.c.sonlist.length; i++){
									var recordedAmountByItem = parseFloat(formObj.c.subGridObj.c.sonlist[i].recordedAmount);
									recordedOperations = recordedOperations+recordedAmountByItem;
								}

								var amountCny = parseFloat(domForm.find('input[name="amountCny"]').val());//CNY金额
								//旧：财务入账金额（CNY） = 总入账金额（CNY）-运营入账金额（CNY）
								//新：财务入账金额（CNY） = CNY金额 - 运营入账金额（CNY）
								var recordedFinance = amountCny - recordedOperations;
								domForm.find('input[name="recordedOperations"]').val(recordedOperations.toFixed(2));
								domForm.find('input[name="recordedFinance"]').val(recordedFinance.toFixed(2));

								formObj.c.subGridObj.reloadGrid();
							} else {
								alert(data.msg);
							}
						}
					});
				//}
			};
		}



		//取消监听
		domPanel.find('button[data-filter="flow_pass"]').off('click');

		// 监听“流转”按钮
		domPanel.find('button[data-filter="flow_pass"]').on('click',function (e) {
			var domThis = $(this);
			var auditingTime = formObj.getValueByName("auditingTime");
			var tipMsg = "确定流转？";
			if(auditingTime==undefined || auditingTime==null || auditingTime==''){
				//对账手续费（CNY）≤计算手续费（CNY） 如不满足，需报错“对账手续费高于计算手续费，请确认是否继续流转”
				var reconciliationFeeTotal = parseFloat(formObj.getValueByName("reconciliationFeeTotal"));//对账手续费（CNY）
				var countFeeTotal = parseFloat(formObj.getValueByName("countFeeTotal"));//计算手续费（CNY）
				if((reconciliationFeeTotal || reconciliationFeeTotal==0) && (countFeeTotal || countFeeTotal==0)
						&& countFeeTotal - reconciliationFeeTotal<0){
					tipMsg = "对账手续费高于计算手续费，请确认是否继续流转";
				}

				//财务入账金额（CNY）≥总入账金额（CNY）*0.998*0.02  如不满足，需报错“财务入账金额不足，请确认是否继续流转”
				var recordedFinance = parseFloat(formObj.getValueByName("recordedFinance"));//财务入账金额（CNY）
				var recordedTotal = parseFloat(formObj.getValueByName("recordedTotal"));//总入账金额（CNY）
				if((recordedFinance || recordedFinance==0) && (recordedTotal || recordedTotal==0)
						&& recordedFinance - recordedTotal*0.998*0.02 <0){
					tipMsg = "财务入账金额不足，请确认是否继续流转";
				}
			}
			layer.confirm(tipMsg, {
						btn: ['确定','取消'] //按钮
					}, function(index){
						var indexOfLoading = layer.load(1, {
							shade: [0.5,'#000000'] //0.1透明度的白色背景
						});
						$.ajax({
							type: "POST",
							url: domThis.attr('data-action'),
							data: {},
							success: function (data) {
								if (data.code == 0) {
									layer.msg(data.msg);
									setTimeout(function(){
										location.reload();
									}, 1000);
								} else {
									alert(data.msg);
								}
								layer.close(indexOfLoading);
							}
						});
					}, function(index){
						layer.close(index);
					}
			);
		});


	});


</script>

</body>
</html>